<?php

namespace App\Service;

use App\Constants\ErrorCode;
use App\Model\DistributionCenter;
use App\Model\LinkFile;
use Hyperf\Snowflake\IdGeneratorInterface;
use Hyperf\Utils\ApplicationContext;
use Psr\Container\ContainerInterface;
use Hyperf\Di\Annotation\Inject;
use App\Model\LinkIndustry;

class DistributionCenterService extends BaseService
{


    public function insert(array $insertData)
    {
        $tags = $insertData['tags'];
        unset($insertData['tags']);
        $honors = $insertData['honors'];
        unset($insertData['honors']);
        $industry = $insertData['industry'];
        unset($insertData['industry']);
        $fileinfo_ids = $insertData['fileinfo_ids'];
        unset($insertData['fileinfo_ids']);
        $center_id = $this->insertGetId($insertData);
        $this->link_insert($tags, $honors, $industry, $fileinfo_ids, $center_id);
        return $center_id;
    }

    public function insertGetId(array $insertData)
    {
        return DistributionCenter::query()->insertGetId($insertData);
    }


    //    更新包括关联关系
    public function update(array $upData)
    {
        $tags = $upData['tags'];
        unset($upData['tags']);
        $honors = $upData['honors'];
        unset($upData['honors']);
        $industry = $upData['industry'];
        unset($upData['industry']);
        $fileinfo_ids = $upData['fileinfo_ids'];
        unset($upData['fileinfo_ids']);
        $center_id = $upData['id'];
        $this->clear_old($center_id);
        $this->link_insert($tags, $honors, $industry, $fileinfo_ids, $center_id);
        return DistributionCenter::query()->where('id', $upData['id'])->update($upData);
    }

    //    清除关联关系 清空旧数据
    public function clear_old(int $center_id)
    {
        LinkIndustry::query()->where(['type' => 1, 'link_id' => $center_id])->delete();
        LinkFile::query()->where(['type' => 1, 'link_id' => $center_id])->delete();
        DistributionCenterTag::query()->where(['type' => 1, 'distribution_center_id' => $center_id])->delete();
    }

    //    插入关联关系
    public function link_insert(array $tags, $honors, $industry, $fileinfo_ids, $center_id)
    {
        //        标签
        $insert_tags = [];
        $industry_data = [];
        $file_data = [];
        foreach ($tags as $v) {
            $insert_tags[] = [
                'distribution_center_id' => $center_id,
                'tags' => $v,
                'type' => 1,
            ];
        }
        foreach ($honors as $v) {
            $insert_tags[] = [
                'distribution_center_id' => $center_id,
                'tags' => $v,
                'type' => 2,
            ];
        }
        //        荣誉 和标签一起插入
        if (count($insert_tags) > 0) {
            DistributionCenterTag::query()->insert($insert_tags);
        }

        //        行业
        foreach ($industry as $v) {
            $industry_data[] = [
                'industry_id' => $v,
                'link_id' => $center_id,
                'type' => 1,
            ];
        }
        if (count($industry_data) > 0) {
            LinkIndustry::query()->insert($industry_data);
        }

//        文件
        foreach ($fileinfo_ids as $v) {
            $file_data[] = [
                'fileinfo_id' => $v,
                'link_id' => $center_id,
                'type' => 1,
            ];
        }
        if (count($file_data) > 0) {
            LinkFile::query()->insert($file_data);
        }
    }


    /**
     * @param int $id
     * @param array|string[] $fields
     * @return array
     */
    public function getDcArrayById(int $id, array $fields = ['*']): array
    {
        $dc = $this->getModelInstance()->where(['id' => $id])->select($fields)->get();
        return empty($dc) ? [] : $dc->toArray();
    }

    /**
     * @param array $id
     * @param array|string[] $fields
     * @return array
     */
    public function getDcArrayByIds(array $id, array $fields = ['*']): array
    {
        $dc = $this->getModelInstance()->whereIn('id', $id)->select($fields)->get();
        return empty($dc) ? [] : $dc->toArray();
    }

}